package org.jeecg.modules.product.job.ticket.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.product.job.ticket.entity.TbJobTicketAuditRecord;
import org.jeecg.modules.product.job.ticket.entity.TbJobTicketEvents;
import org.jeecg.modules.product.job.ticket.entity.TbJobTicketEventsOperatingReserve;
import org.jeecg.modules.product.job.ticket.mapper.TbJobTicketAuditRecordMapper;
import org.jeecg.modules.product.job.ticket.mapper.TbJobTicketEventsMapper;
import org.jeecg.modules.product.job.ticket.mapper.TbJobTicketEventsOperatingReserveMapper;
import org.jeecg.modules.product.job.ticket.service.ITbJobTicketEventsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 电子作业票活动清单
 * @Author: jeecg-boot
 * @Date:   2024-08-08
 * @Version: V1.0
 */
@Service
public class TbJobTicketEventsServiceImpl extends ServiceImpl<TbJobTicketEventsMapper, TbJobTicketEvents> implements ITbJobTicketEventsService {
    @Autowired
    private TbJobTicketEventsMapper tbJobTicketEventsMapper;
    @Autowired
    private TbJobTicketEventsOperatingReserveMapper tbJobTicketEventsOperatingReserveMapper;
    @Autowired
    private TbJobTicketAuditRecordMapper tbJobTicketAuditRecordMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMain(TbJobTicketEvents tbJobTicketEvents, TbJobTicketEventsOperatingReserve tbJobTicketEventsOperatingReserve, List<TbJobTicketAuditRecord> tbJobTicketAuditRecordList) {
        tbJobTicketEventsMapper.insert(tbJobTicketEvents);
        if(tbJobTicketEventsOperatingReserve!=null) {
            //外键设置
            tbJobTicketEventsOperatingReserve.setJobTicketEventsId(tbJobTicketEvents.getId());
            tbJobTicketEventsOperatingReserveMapper.insert(tbJobTicketEventsOperatingReserve);
        }
        if(tbJobTicketAuditRecordList!=null && tbJobTicketAuditRecordList.size()>0) {
            for(TbJobTicketAuditRecord entity:tbJobTicketAuditRecordList) {
                //外键设置
                entity.setJobTicketId(tbJobTicketEvents.getId());
                tbJobTicketAuditRecordMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMain(TbJobTicketEvents tbJobTicketEvents,TbJobTicketEventsOperatingReserve tbJobTicketEventsOperatingReserve,List<TbJobTicketAuditRecord> tbJobTicketAuditRecordList) {
        tbJobTicketEventsMapper.updateById(tbJobTicketEvents);

        //1.先删除子表数据
        tbJobTicketEventsOperatingReserveMapper.deleteByMainId(tbJobTicketEvents.getId());
        tbJobTicketAuditRecordMapper.deleteByMainId(tbJobTicketEvents.getId());

        //2.子表数据重新插入
        if(tbJobTicketEventsOperatingReserve!=null) {
            //外键设置
            tbJobTicketEventsOperatingReserve.setJobTicketEventsId(tbJobTicketEvents.getId());
            tbJobTicketEventsOperatingReserveMapper.insert(tbJobTicketEventsOperatingReserve);
        }
        if(tbJobTicketAuditRecordList!=null && tbJobTicketAuditRecordList.size()>0) {
            for(TbJobTicketAuditRecord entity:tbJobTicketAuditRecordList) {
                //外键设置
                entity.setJobTicketId(tbJobTicketEvents.getId());
                tbJobTicketAuditRecordMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delMain(String id) {
        tbJobTicketEventsOperatingReserveMapper.deleteByMainId(id);
        tbJobTicketAuditRecordMapper.deleteByMainId(id);
        tbJobTicketEventsMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for(Serializable id:idList) {
            tbJobTicketEventsOperatingReserveMapper.deleteByMainId(id.toString());
            tbJobTicketAuditRecordMapper.deleteByMainId(id.toString());
            tbJobTicketEventsMapper.deleteById(id);
        }
    }
}
